package com.party.controller.api;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.party.common.base.ApiResult;
import com.party.common.entity.MyPage;
import com.party.entity.PbDevice;
import com.party.entity.PbUser;
import com.party.entity.dto.DistributeDto1;
import com.party.entity.dto.QueryDeviceDto;
import com.party.entity.vo.ExportResourceVo;
import com.party.entity.vo.SelectDeviceVo;
import com.party.service.PbDeviceService;
import com.party.service.PbUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 设备管理 前端控制器
 * </p>
 *
 * @author ccl
 * @since 2024-10-25
 */
@RestController
@RequestMapping("api/pb-device")
@Api(tags = "设备相关api接口", value = "设备相关api接口")
public class PbDeviceController {

    @Resource
    private PbDeviceService pbDeviceService;

    @Resource
    private PbUserService pbUserService;

    @PostMapping("add")
    @ApiOperation("保存")
    public ApiResult add(@RequestBody PbDevice pbDevice){
        return ApiResult.ok(pbDeviceService.add(pbDevice));

    }

    @PostMapping("update")
    @ApiOperation("编辑")
    public ApiResult update(@RequestBody PbDevice pbDevice){
        return ApiResult.ok(pbDeviceService.edit(pbDevice));
    }

    @GetMapping("delete")
    @ApiOperation("删除")
    public ApiResult delete(Long id){
        return ApiResult.ok(pbDeviceService.delete(id));
    }

    @PostMapping("queryPage")
    @ApiOperation("分页查询")
    public ApiResult<MyPage<PbDevice>> queryPage(@RequestBody QueryDeviceDto dto){
        MyPage<PbDevice> ccpSystemUserMyPage = pbDeviceService.queryPage(dto);
        return ApiResult.ok(ccpSystemUserMyPage);
    }

    @GetMapping("detail")
    @ApiOperation(value = "详情",response = PbUser.class)
    public ApiResult detail(Long id){
        return ApiResult.ok(pbDeviceService.detail(id));
    }

    @PostMapping("distribute")
    @ApiOperation(value = "给设备分配资源和电台 分配类型 0-资源 1-自控电台 2-党建风采 3-轮播海报 4-主题日历",response = Boolean.class)
    public ApiResult<Boolean> distribute(@RequestBody DistributeDto1 dto) {
        return ApiResult.ok(pbDeviceService.distribute(dto));
    }

    @GetMapping("selectDevice")
    @ApiOperation(value = "设备下拉框",response = Boolean.class)
    public ApiResult<List<SelectDeviceVo>> selectDevice(Long id) {
        return ApiResult.ok(pbUserService.selectDevice(id));
    }

    @GetMapping("/downTemplate")
    @ResponseBody
    @ApiOperation(value = "资源导入模板下载",response = ApiResult.class)
    public void downTemplate(HttpServletResponse response) throws IOException {
        List<ExportResourceVo> list = new ArrayList<>();
        EasyExcel.write(response.getOutputStream(),ExportResourceVo.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("资源列表")
                .doWrite(list);
    }

    @PostMapping("/importDistribute")
    @ApiOperation(value = "资源导入分配",response = ApiResult.class)
    public ApiResult importDistribute(@RequestParam("file") MultipartFile file,@RequestParam("id") String id) throws Exception {
        return pbUserService.importDistribute(file,id);
    }
}

